public class emiratesCityAreaLookupController 
{
    public Account act {get;set;} // new account to create
    public Emirates__c emiratesObject {get;set;} // New Emirates to create...
    public City__c emiratesCityObject {get;set;} // New Emirates City to create...
    public Area__c emiratesCityAreaObject {get;set;} // New Emirates City Area to create...
    
    public List<Account> results{get;set;} // search results
    public List<Emirates__c> emiratesResultList{get;set;} // search Emirates Results
    public List<City__c> emiratesCityResultList{get;set;} // search Emirates City Results
    public List<Area__c> emiratesCityAreaResultList{get;set;} // search Emirates City Results
    
    public Boolean emiratesFlag {get;set;}
    public Boolean cityFlag {get;set;}
    public Boolean areaFlag {get;set;}
    
    public String processIdentifier {get;set;}
    
    public string searchString{get;set;} // search keyword
    
    public string emiratesName {get;set;}
    public string cityName {get;set;}
 
    public emiratesCityAreaLookupController() 
    {
        act = new Account();
        processIdentifier = '';
        emiratesObject  = new Emirates__c();
        emiratesCityObject = new City__c();
        emiratesCityAreaObject = new Area__c();
        
        emiratesFlag = false;
        cityFlag = false;
        areaFlag = false;
        
        emiratesResultList = new List<Emirates__c>();
        emiratesCityResultList = new List<City__c>();
        emiratesCityAreaResultList = new List<Area__c>();
        
        emiratesName = '';
        cityName = '';
        
        if(ApexPages.CurrentPage().getParameters().get('emiratesName') != null)
            emiratesName  = ApexPages.CurrentPage().getParameters().get('emiratesName');
        
        if(ApexPages.CurrentPage().getParameters().get('cityName') != null)
            cityName = ApexPages.CurrentPage().getParameters().get('cityName');
        
        if(ApexPages.CurrentPage().getParameters().get('processName') != null)
        { 
            processIdentifier = ApexPages.CurrentPage().getParameters().get('processName');
            
            if(processIdentifier == 'Emirates')
                emiratesFlag  = true;
            else if (processIdentifier == 'City')    
                cityFlag = true;
            else if (processIdentifier == 'Area')    
                areaFlag = true;
                    
            // get the current search string
            searchString = System.currentPageReference().getParameters().get('lksrch');
            
            runSearch();  
            
        
        }
        
    }
 
    // performs the keyword search
    public PageReference search() 
    {    
        if(processIdentifier != '' && processIdentifier != null)
        {
            runSearch();
        }
        return null;
    }
 
    // prepare the query and issue the search command
    private void runSearch() 
    {
        // TODO prepare query string for complex serarches & prevent injections
       
        if(processIdentifier == 'Emirates')
            emiratesResultList = performEmiratesSearch(searchString);
        else if (processIdentifier == 'City' && emiratesName != '')    
            emiratesCityResultList = performEmiratesCitySearch(searchString);
        else if (processIdentifier == 'Area' && cityName != '')    
            emiratesCityAreaResultList = performEmiratesCityAreaSearch(searchString);
            
            
        System.debug('The Emirates => '+ emiratesResultList.size());    
             
    } 
 
    // run the search and return the records found. 
    private List<Emirates__c> performEmiratesSearch(string searchString) 
    {

        String soql = 'SELECT id, name FROM Emirates__c WHERE Active__c = true ';
            
        if(searchString != '' && searchString != null)
        {
            soql = soql +  ' AND name LIKE \'' + searchString +'%\'';
        }
        soql = soql + ' limit 50';
        System.debug(soql);
        return database.query(soql); 
    }
  
    // run the search and return the records found. 
    private List<City__c> performEmiratesCitySearch(string searchString) 
    {

        String soql = 'SELECT id, name FROM City__c WHERE Active__c = true AND Emirates__r.Name = \''+ emiratesName + '\'';
        
            
        if(searchString != '' && searchString != null)
        {
            soql = soql +  ' AND name LIKE \'' + searchString +'%\'';
        }
        soql = soql + ' limit 50';
        System.debug(soql);
        return database.query(soql); 
    }  
  
    // run the search and return the records found. 
    private List<Area__c> performEmiratesCityAreaSearch(string searchString) 
    {

        String soql = 'SELECT id, name FROM Area__c WHERE Active__c = true AND City__r.Name = \''+ cityName + '\'';
        
            
        if(searchString != '' && searchString != null)
        {
            soql = soql +  ' AND name LIKE \'' + searchString +'%\'';
        }
        soql = soql + ' limit 50';
        System.debug(soql);
        return database.query(soql); 
    }
  
  
    // used by the visualforce page to send the link to the right dom element
    public string getFormTag() 
    {
        return System.currentPageReference().getParameters().get('frm');
    }
 
    // used by the visualforce page to send the link to the right dom element for the text box
    public string getTextBox() 
    {
        return System.currentPageReference().getParameters().get('txt');
    }
    
    
    //Test Methods...
    
    public static testMethod void testMyController() 
    {
    	
    	//For Emirates Name...
    	PageReference Pg = null;
   		
    	ApexPages.currentPage().getParameters().put('processName','Emirates' );
   		emiratesCityAreaLookupController  controller = new emiratesCityAreaLookupController ();
   		
   		Pg = controller.search();
   		String formTag  = controller.getFormTag();
       	String textBox = controller.getTextBox();
       	
       	ApexPages.currentPage().getParameters().put('processName','City' );
       	ApexPages.currentPage().getParameters().put('emiratesName','Test' );
   		emiratesCityAreaLookupController  controller1 = new emiratesCityAreaLookupController ();
   		Pg = controller1.search();
   		
   		ApexPages.currentPage().getParameters().put('processName','Area' );
       	ApexPages.currentPage().getParameters().put('cityName','Test' );
   		emiratesCityAreaLookupController  controller2 = new emiratesCityAreaLookupController ();
   		Pg = controller2.search();
       	
       	  
    }
}